package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.ui.activity.DesignerHmePageActivity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/4/6
 * Time: 14:49
 */

public class DailyExhibitionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<CreativeProductRequestEntity> mDatas = new ArrayList<>();


    public void addAllData(List<CreativeProductRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public DailyExhibitionAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView user_name, show_title, show_content;
        ImageView cover_img, head_img;
        RelativeLayout designer_details;

        public ViewHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            show_title = (TextView) itemView.findViewById(R.id.show_title);
            show_content = (TextView) itemView.findViewById(R.id.show_content);
            cover_img = (ImageView) itemView.findViewById(R.id.cover_img);
            head_img = (ImageView) itemView.findViewById(R.id.head_img);
            designer_details= (RelativeLayout) itemView.findViewById(R.id.designer_details);
     //       initListener(itemView);
        }

//        private void initListener(View itemView) {
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(mContext, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_daily_exhibition, parent, false);
        return new DailyExhibitionAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DailyExhibitionAdapter.ViewHolder itemViewHolder = (DailyExhibitionAdapter.ViewHolder) holder;
        itemViewHolder.user_name.setText(mDatas.get(position).getNickName());
        itemViewHolder.show_title.setText(mDatas.get(position).getProductTitle());
        itemViewHolder.show_content.setText(mDatas.get(position).getProductDesc());
        String imgUrl = mDatas.get(position).getProductCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.cover_img);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.cover_img);
            }
        }else{
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.cover_img);

        }

        String imgUrl2 = mDatas.get(position).getHeadImg();

        if (imgUrl2 != null) {
            if (!imgUrl2.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl2, itemViewHolder.head_img);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.me_img_default, itemViewHolder.head_img);
            }
        }
        itemViewHolder.designer_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, DesignerHmePageActivity.class) ;
                intent.putExtra("dUserId",mDatas.get(position).getUserId());
                intent.putExtra("type","2");
                mContext.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

}
